package com.yc.navy.goods.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.goods.bean.FindByPageInfo;
import com.yc.navy.goods.bean.OrderInfo;
import com.yc.navy.goods.bean.OrderItemInfo;
import com.yc.navy.goods.dto.AddressInfoDTODTO;
import com.yc.navy.goods.dto.BalanceInfoDTO;
import com.yc.navy.goods.dto.ConstantInfoDTO;
import com.yc.navy.goods.dto.MemberInfoDTO;
import com.yc.navy.goods.dto.OrderDTO;
import com.yc.navy.goods.dto.OrderInfoDTO;
import com.yc.navy.goods.dto.OrderItemInfoShopList;
import com.yc.navy.goods.dto.PayInfoDTO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.feign.IUserFeignClient;
import com.yc.navy.goods.product.feign.IProductFeignClient;
//import com.yc.navy.goods.service.IAddressInfoService;
//import com.yc.navy.goods.service.IBalanceInfoService;
import com.yc.navy.goods.service.IOrderInfoService;
import com.yc.navy.goods.service.IOrderItemInfoService;
import com.yc.navy.goods.util.StringUtil;
import com.yc.navy.goods.vo.ResultVO;
import com.yc.navy.goods.vo.StreamVO;

@RestController
@RequestMapping("/order")
public class OrderInfoController {
	@Autowired
	private IOrderInfoService orderInfoService;
	
	//@Autowired
	//private IAddressInfoService addressInfoService;
	
	@Autowired
	private RedisTemplate<String,Object> redisTemplate;
	
	@Autowired
	private IOrderItemInfoService orderItemInfoService;
	
	//@Autowired
	//private IBalanceInfoService balanceInfoService;
	
	@Autowired
	private IUserFeignClient userFeignClient;
	
	@Autowired
	private IProductFeignClient prouductFeignClient;

	
	private String getlogin(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfoDTO.LOGINCHECK);
		if(obj == null) {
			return null;
		}
		
		MemberInfoDTO m = (MemberInfoDTO) obj;
		return m.getMid() +"";
	}
	
	@RequestMapping("/add")
	public ResultVO add(@RequestParam("bid") Integer bid,@RequestParam("totalprice") String totalprice,HttpSession session,@RequestParam(value = "aid",required = false) String aid,@RequestParam(value = "num",required = false) String num) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		AddressInfoDTODTO address = new AddressInfoDTODTO();
		if(aid == null || aid == "") {
			address = userFeignClient.myaddr(Integer.parseInt(mid));
			System.out.println("address&&&& = " + address);
		}else {
			address.setAid(Long.parseLong(aid));
			System.out.println("address***** = " + address);
		}
		
		if(String.valueOf(address.getAid()) == null) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		if(StringUtil.checkNull(totalprice)) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		
		
		OrderInfo order = new OrderInfo();
		order.setMid(Integer.parseInt(mid));
		order.setAid(address.getAid());
		order.setTotalprice(Double.parseDouble(totalprice));
		int result = orderInfoService.add(order);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		
//		Map<Object, Object> map = redisTemplate.opsForHash().entries("m_"+mid);
//		if(map.isEmpty() || map.size() <= 0) {
//			return new ResultVO(ResultEnum.NOT_DATA);
//		}
		int oid = orderInfoService.findoid(Integer.parseInt(mid));//
		if(oid <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		OrderItemInfo orderitem = new OrderItemInfo();
		orderitem.setOid(oid);
		
		if(StringUtil.checkNull(num,String.valueOf(bid))) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		orderitem.setNum(Integer.parseInt(num));
		orderitem.setBid(bid);
		
		BalanceInfoDTO balanceDTO = new BalanceInfoDTO();
		balanceDTO.setNum(Integer.parseInt(num));
		balanceDTO.setBid(bid);
		
		int result2 = orderItemInfoService.add(orderitem);
		System.out.println(balanceDTO + "balanceDTO");
		
		if(result2 <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
//		if(result3 <= 0 ) {
//			return new ResultVO(ResultEnum.NOT_NUM);
//		}
		PayInfoDTO pay = new PayInfoDTO();
		pay.setMid(Integer.parseInt(mid));
		pay.setOid(oid);
		pay.setTotalprice(Double.valueOf(totalprice));
		
		prouductFeignClient.updateNum(balanceDTO);
		return new ResultVO(ResultEnum.SUCCESS,pay);
		
	}
	
	@RequestMapping("/addlist")
	public ResultVO add(@RequestParam("pnos") String[] pnos,@RequestParam("totalprice") String totalprice,HttpSession session,@RequestParam(value = "aid",required = false) Long aid) {
		String mid = getlogin(session);
		if(mid == null) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		AddressInfoDTODTO address = new AddressInfoDTODTO();
		
		if(String.valueOf(aid) == null || String.valueOf(aid) == "") {
			address = userFeignClient.myaddr(Integer.parseInt(mid));
		}else{
			address.setAid(aid);
		}
		
		if(String.valueOf(address.getAid()) == null) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		if(StringUtil.checkNull(totalprice)) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		
		
		OrderInfo order = new OrderInfo();
		order.setMid(Integer.parseInt(mid));
		order.setAid(address.getAid());
		order.setTotalprice(Double.parseDouble(totalprice));
		int result = orderInfoService.add(order);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		
		int oid = orderInfoService.findoid(Integer.parseInt(mid));
		if(oid <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		OrderItemInfo orderitem = new OrderItemInfo();
		orderitem.setOid(oid);
		
		Map<Object, Object> map = redisTemplate.opsForHash().entries("m_"+mid);
		if(map.isEmpty() || map.size() <= 0) {
			return new ResultVO(ResultEnum.NOT_DATA);
		}
		int result2 = 0;
		int result3 = 0 ;
		for(String sbid : pnos) {
			orderitem.setNum(Integer.parseInt(String.valueOf(map.get("b_"+sbid))));
			orderitem.setBid(Integer.parseInt(String.valueOf(sbid)));
			result2 = orderItemInfoService.add(orderitem);
			
			BalanceInfoDTO balanceDTO = new BalanceInfoDTO();
			balanceDTO.setNum(Integer.parseInt(String.valueOf(map.get("b_"+sbid))));
			balanceDTO.setBid(Integer.parseInt(String.valueOf(sbid)));
			//result3 = prouductFeignClient.updateNum(balanceDTO);
			if(result2 <= 0) {
				return new ResultVO(ResultEnum.ERROR);
			}
			if(result3 <= 0) {
				return new ResultVO(ResultEnum.NOT_NUM);
			}
			redisTemplate.opsForHash().delete("m_"+mid, "b_"+sbid);
		}
		OrderInfoDTO orderdto = new OrderInfoDTO();
		orderdto.setOid(oid);
		orderdto.setTotalprice(Integer.parseInt(totalprice));
		orderdto.setMid(Integer.parseInt(mid));
		return new ResultVO(ResultEnum.SUCCESS,orderdto);
		
}
	@RequestMapping("/finds")
	public ResultVO finds(HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)){
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		List<OrderDTO> list = orderInfoService.finds(Integer.parseInt(mid));
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/findList")
	public StreamVO findList(@RequestParam Map<String,Object> map) {
		return orderInfoService.findList(map);
	}
	
	@RequestMapping("/shoperfindList")
	public ResultVO shoperfindList(Integer sid,Integer page, Integer pageSize,HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		FindByPageInfo bypage = new FindByPageInfo();
		bypage.setId(sid);
		bypage.setPage((page - 1) * pageSize);
		bypage.setRows(pageSize);
		
		List<OrderItemInfoShopList> list = orderInfoService.shoperfindList(bypage);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/shopertotal")
	public ResultVO shopertotal(Integer sid) {
		int result = orderInfoService.shopertotal(sid);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS,result);
	}
	
	@RequestMapping("/updateOstatus")
	public ResultVO updateOstatus(Integer oid) {
		int result = orderInfoService.updateOstatus(oid);
		if(result <= 0 ) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
}